package yto.freemarker.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import yto.freemarker.springcloud.entity.User;

/**
 * @author dongxing
 * @create 2017-12-24 17:00
 **/
@RestController
public class UserController {

    @Autowired
    private RestTemplate restTemplate;


    @HystrixCommand(fallbackMethod = "fallTestMethod")
    @GetMapping("/simple/{id}")
    public User findById(@PathVariable Long id) {
        return restTemplate.getForObject("http://service-provider-movie/simple/" + id, User.class);
    }

    /**
     * 方法的参数和返回值类型需要和原来的方法相对应的保持一致性
     *
     * @param id
     * @return
     */
    public User fallTestMethod(Long id) {
        User user = new User();
        user.setId(0L);
        user.setUsername("牛德华");
        return user;
    }
}
